<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Pro;
use App\Att;
use App\Bid;

use Auth;
use DB;
use Validator;

class ProController extends Controller
{
        //检测用户是否登录,系统已将中间件封装好,直接使用即可
        protected $middleware = ['App\Http\Middleware\Authenticate'=>[]];
       //展示模板
       public function jie(){
            return view('woyaojiekuan');
       }

       public function jiePost(Request $req){
            /*$this->validate($req, [
                'age' => 'required|in:15,40,80',
                'money' => 'required|digits_between:2,7',
                'mobile' =>'required|regex:/^1[3458]\d{9}$/'
                ],
                [
                 'age.required' => '年龄必须填',
                 'age.in' => '在15,40,80之间选',
                 'money.required' => '金额必须填',
                 'money.digits_between' => '必须是数字而且是2到7位',
                 'mobile.required' => '手机号必须填',
                 'mobile.regex' => '要符合手机号规则'
                ]);*/
            $validate = validator::make($req->all(),
                [
                'age' => 'required | in:15,40,80',
                'money' => 'required | digits_between:2,7',
                'mobile' => 'required | regex:/^1[3458]\d{9}$/'
                ],
                [
                'age.required' => '年龄必须填',
                'age.in' => ':attribute 必须在15,40,80之间选',
                'money.required' => '金额必须填',
                'money.digits_between' => ':attribute 必须是数字并且是2到7位',
                'mobile.required' => '手机号必须填',
                'mobile.regex' => ':attribute 要符合手机号规则'
                ]);
            //判断是否失败
            if($validate->fails()){
                //会退到上一页,且携带数据和上次表单的旧数据
                return back()->withErrors($validate)->withInput();
            }
            // $req = new Request();
            //调用model下的Pro
            $pro = new Pro();
            //调用model下的Att
            $att = new Att();
            //获取当前登录用户的对象
            // $user = Auth::user();
            $user = $req->user();
            //写入主表信息projects
            $pro->uid = $user->uid;
            $pro->name = $user->name;
            //转化成分
            $pro->money = intval($req->money)*100;
            $pro->mobile = $req->mobile;
            $pro->pubtime = time();
            $pro->save();

            //写入附表信息atts
            $att->uid = $pro->uid;
            $att->age = $req->age;
            $att->pid = $pro->pid;
            $att->save();

            return '借款成功';



       }

        //投标页
        public function pro($pid){

            $pro = Pro::where('pid',$pid)->first();
            // dd($pro);
            return view('liketouzi',['pro'=>$pro]);
        }

        //写入投资信息
        public function touzi(Request $req,$pid){
            // dd($req);
            $pro = Pro::find($pid);
            // dd($pro);
            $bid = new Bid();
            // dd($bid);
            $user = Auth::user();
            // $user = $req->user();
            // dd($user);
            // if($req->money > ($pro->money-$pro->recive)/100){
            //     return '金额不能过多,不行!!!';
            // }
            // if($req->money < 0){
            //     return '不行';
            // }
             //dd($req);
            $md5 = $req->v_oid.$req->v_pstatus.$req->v_amount.$req->v_moneytype.'%()#QOKFDLS:1*&U';
            // echo $req->v_oid . $req->v_pstatus .$req->v_amount. $req->moneytype;
            $md5 = strtoupper(md5($md5));
            //dd($md5);

            if($md5 !== $req->v_md5str){
                return '签名错误';
            }

            $bid->uid = $user->uid;
            $bid->pid = $pro->pid;
            $bid->title = $pro->title;
            $bid->money = $req->v_amount*100;
            $bid->pubtime = time();
            $bid->save();

            $pro->recive += $bid->money;
            $pro->save();

            if($pro->recive === $pro->money){
                $this->touziDone($pid);
            }
            return '购买成功';
        }

        //还款表和收益表
        protected function touziDone($pid){
            //1.修改状态为2,还款收益中
            $pro = Pro::find($pid);
            $pro->status = 2;
            $pro->save();

            //2.为借款者,生成还款记录
            //每月应还钱数,(本金+利息) 利率按百分比算,除以100
            $amount = ($pro->money / $pro->hrange) + ($pro->money * $pro->rate) / 1200;
            $row = ['uid'=>$pro->uid,'pid'=>$pro->pid,'title'=>$pro->title];
            $row['amount'] = $amount;
            $row['status'] = 0;

            //循环产生每月还款记录
            for($i=1;$i<=$pro->hrange;$i++){
                $paydate = date('Y-m-d',strtotime("+ $i months")); //下一个月的日期
                $row['paydate'] = $paydate;
                DB::table('hks')->insert($row);
            }
            

            //3.为投资者,生成收益记录
            $bid = Bid::where('pid',$pid)->get();//查询投资者信息
            $row = [];
            $row['pid'] = $pid;
            $row['title'] = $pro->title;
            //收益截止日
            $row['enddate'] = date('Y-m-d',strtotime("+ {$pro->hrange} months"));
            //循环为每位投资者生成预期收益信息
            foreach($bid as $b){
                $row['uid'] = $b->uid;
                $row['amount'] = $b->money * $pro->rate / 36500;
                DB::table('tasks')->insert($row);
            }
        }

        //在线支付
        public function pay(Request $req){
            $pro = Pro::find($req->pid);
            // dd($pro);
            if($req->money > ($pro->money-$pro->recive)/100){
                return '金额不能过多,不行!!!';
            }
            if($req->money < 0){
                return '不行';
            }
            // dd($req);
            $row = [];
            $row['v_amount'] = sprintf('%.2f',$req->money);
            $row['v_moneytype'] = 'CNY';
            $row['v_oid'] = date('Ymd',mt_rand(1000,9999));
            $row['v_mid'] = '20272562';
            $row['v_url'] = 'http://myblog.com/touzi/'.$req->pid;
            $row['key'] = '%()#QOKFDLS:1*&U';

            $row['v_md5info'] = strtoupper(md5(implode('',$row)));
            //dd($row);
            //['v_amount'=> sprintf('%,2f',$req->money),]
            return view('pay',$row);
        }

        //还款账单
        public function myzd(){

            $user = Auth::user();
            $hks = DB::table('hks')->where('uid',$user->uid)->paginate(2);
            // dd($hks);
            return view('myzd',['hks'=>$hks]);
        }

        //投资账单
        public function mytz(){

            $user = Auth::user();
            $bids = Bid::where('bids.uid',$user->uid)->whereIn('status',[1,2])->join('projects','bids.pid','=','projects.pid')->select(['bids.*','projects.status'])->paginate(2);
            // dd($bids);
            return view('mytz',['bids'=>$bids]);    
        }

        //收益账单
        public function mysy(){
            $user = Auth::user();
            $grows = DB::table('grows')->where('uid',$user->uid)->orderBy('gid','desc')->paginate(2);
            return view('mysy',['grows'=>$grows]);
        }
       
    }
